package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;


import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.UserBo;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;


public class NewsChangeNikeNameActivity extends BaseActivity {
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.etNickname)
    EditText etNickname;

    @Override
    public void setListen() {

    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        ivTitleRight.setVisibility(View.GONE);
        new TitleBuilder(this).setTitleText(getString(R.string.setNikeName))
                .setRightText(getString(R.string.done))
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });


    }

    @Override
    public int intiLayout() {
        return R.layout.activity_new_changenikename;
    }

    @Override
    public void initView() {

    }

    @OnClick({R.id.tv_title_right})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_title_right://完成
                if (etNickname.getText().toString().length() > 0){
                    insertData();
                }else {
                    NewsToastUtils.showToast(NewsChangeNikeNameActivity.this,"昵称不能为空！");
                }
                break;
        }
    }

    //插入到数据库
    private void insertData() {
        String avatar = SPUtils.get(App.getContext(), "avatar", "") + "";
        String userId = SPUtils.get(App.getContext(), "userId", "") + "";
        String nikeName =etNickname.getText().toString();
        UserBo userBo = new UserBo();
        userBo.setAvatar(avatar);
        userBo.setNickname(nikeName);
        userBo.setUserid(userId);
        Task.getApiService().insertData(userBo).enqueue(new MyCallback<NewsBaseEntity>(NewsChangeNikeNameActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                try {
                    if ("1".equals(response.body().getCode())) {
                        NewsToastUtils.showToast(NewsChangeNikeNameActivity.this,"修改成功！");
                        if (etNickname.getText().toString().length() > 0) {
                            Intent it = new Intent();
                            it.putExtra("nickname", etNickname.getText().toString().trim());
                            setResult(RESULT_OK, it);
                        }
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    @Override
    public void initData() {
        String nickname = getIntent().getStringExtra("nickname");
        etNickname.setText(nickname);
        assert nickname != null;
        etNickname.setSelection(nickname.length());
    }
}
